<template>
  <div class="learning-create">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>学习记录新增</span>
          <el-button type="primary" @click="showDialog = true">新增学习记录</el-button>
        </div>
      </template>
      <p>记录学生学习：知识点、起始时间、学习时长（分钟）、课程名称。</p>
    </el-card>

    <el-dialog v-model="showDialog" title="新增学习记录" width="560px" @close="resetForm">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入姓名" clearable />
        </el-form-item>
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="form.studentNo" placeholder="请输入学号" clearable />
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" clearable />
        </el-form-item>
        <el-form-item label="知识点" prop="knowledgePoint">
          <el-input v-model="form.knowledgePoint" placeholder="请输入知识点" clearable />
        </el-form-item>
        <el-form-item label="起始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="选择起始时间"
          />
        </el-form-item>
        <el-form-item label="学习时长(分钟)" prop="durationMinutes">
          <el-input-number v-model="form.durationMinutes" :min="1" :max="600" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" :loading="submitting" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { learningRecordApi } from '@/api/learningRecord'

export default {
  name: 'LearningRecordCreate',
  setup() {
    const showDialog = ref(false)
    const submitting = ref(false)
    const formRef = ref()
    const form = reactive({
      studentName: '',
      studentNo: '',
      courseName: '',
      knowledgePoint: '',
      startTime: '',
      durationMinutes: 60
    })

    const rules = {
      studentName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
      studentNo: [{ required: true, message: '请输入学号', trigger: 'blur' }],
      courseName: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
      knowledgePoint: [{ required: true, message: '请输入知识点', trigger: 'blur' }],
      startTime: [{ required: true, message: '请选择起始时间', trigger: 'change' }],
      durationMinutes: [{ required: true, message: '请输入时长', trigger: 'blur' }]
    }

    const submit = () => {
      if (!formRef.value) return
      formRef.value.validate(async (valid) => {
        if (!valid) return
        submitting.value = true
        try {
          const fd = new FormData()
          fd.append('studentName', form.studentName)
          fd.append('studentNo', form.studentNo)
          fd.append('courseName', form.courseName)
          fd.append('knowledgePoint', form.knowledgePoint)
          fd.append('startTime', form.startTime)
          fd.append('durationMinutes', form.durationMinutes)
          const res = await learningRecordApi.create(fd)
          if (res.data?.code === 200) {
            ElMessage.success(res.data?.message || '保存成功')
            showDialog.value = false
            resetForm()
          } else {
            ElMessage.error(res.data?.message || '保存失败')
          }
        } catch (e) {
          ElMessage.error(e?.response?.data?.message || e.message || '保存失败')
        } finally {
          submitting.value = false
        }
      })
    }

    const resetForm = () => {
      if (formRef.value) formRef.value.resetFields()
      form.studentName = ''
      form.studentNo = ''
      form.courseName = ''
      form.knowledgePoint = ''
      form.startTime = ''
      form.durationMinutes = 60
    }

    return {
      showDialog,
      submitting,
      formRef,
      form,
      rules,
      submit,
      resetForm
    }
  }
}
</script>

<style scoped>
.learning-create { padding: 20px; }
.card-header { display: flex; justify-content: space-between; align-items: center; }
</style>